<template>
    <div class="content">
        <div class="clear">
            <Form inline class="fl">
                <Input type="text" placeholder="请输入ID" class="w200 m-r10" v-model="userId" />
                <DatePicker type="daterange" split-panels placeholder="请选择时间段" class="w200 m-r10" format="yyyy-MM-dd HH:mm:ss" @on-change="getTime">
                </DatePicker>
                <Button type="primary" @click="query">查询</Button>
            </Form>
        </div>
        <fieldset>
            <legend>汇总</legend>
            <div>
                <table>
                    <tbody>
                      <tr>
                        <td>累积全网收益：
                            <span class="blue">{{ btcsum == null ? 0 : btcsum }}</span>
                        </td>
                      </tr>
                    </tbody>
                </table>
            </div>
        </fieldset>
        <Button class="daochu" type="primary" size="large" @click="exportData">
            <Icon type="ios-download-outline"></Icon> 导出</Button>
        <Table stripe :columns="columns1" :data="btcList" ref="table" v-show="btc"></Table>
        <Page :total="info" show-elevator :page-size="pageSize" show-total  @on-change="changepage" v-show="fenye"></Page>
        <Page :total="info1" show-elevator :page-size="pageSize" show-total  @on-change="changepage1" v-show="fenye1"></Page>
    </div>
</template>
<script>
export default {
  data () {
    return {
      btc: true,
      eth: false,
      fenye: true,
      fenye1: false,
      bizhong: '1',
      userId: '',
      btcList: [],
      ethList: [],
      btcsum: null,
      ethsum: null,
      dataValue: [], // 时间段值
      // // 分页
      info: null, // 总条数
      info1: null, // 总条数
      page: 1, // 页数
      pageSize: 20, // 每页多少条
      columns1: [
        {
          title: '个人ID',
          align: 'center',
          key: 'childUuid'
        },
        {
          title: '个人手机号码',
          align: 'center',
          key: 'phone',
          render: (h, params) => {
            return h('div', params.row.phone == null ? '#' : params.row.phone)
          }
        },
        {
          title: '全网收益',
          align: 'center',
          key: 'rate'
        },
        {
          title: '收益占比',
          align: 'center',
          key: 'level'
        },
        {
          title: '收益时间',
          align: 'center',
          key: 'earningsTime',
          sortable: true
        }
      ]
    }
  },
  methods: {
    exportData () {
      this.$refs.table.exportCsv({
        filename: 'BTC量化宝推荐收益'
      })
      this.$refs.table1.exportCsv({
        filename: 'ETH量化宝推荐收益'
      })
    },
    // 处理时间段时间格式
    getTime (daterange) {
      this.dataValue = daterange
    },
    // 获取btc表格数据
    getBList () {
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        currencyType: 2,
        pn: this.page,
        pageSize: this.pageSize
      })
      this.axios.post('statistics/quantitativeTreasure', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        // console.log(res)
        if (res.data.code == 8008) {
          this.btcList = res.data.data.earningsList
          this.info = res.data.data.total
          this.btcsum = res.data.data.goBtcEarningsSum
          this.ethsum = res.data.data.goEthEarningsSum
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({name: 'login'})
        }
      })
    },
    // 获取eth表格数据
    getEList () {
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        currencyType: 1,
        pn: this.page,
        pageSize: this.pageSize
      })
      this.axios.post('statistics/quantitativeTreasure', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        // console.log(res)
        if (res.data.code == 8008) {
          this.ethList = res.data.data.earningsList
          this.info1 = res.data.data.total
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({name: 'login'})
        }
      })
    },
    // 查询
    query () {
      // console.log(this.bizhong)
      if (this.bizhong == 2) {
        this.btc = false
        this.eth = true
        this.fenye = false
        this.fenye1 = true

        if ((this.dataValue == undefined || this.dataValue[0] == '' || this.dataValue.length == 0) && this.userId == '') {
          this.pageSize = 20
          this.getEList()
          return
        }
        this.pageSize = 100
        let token = localStorage.getItem('mytoken')
        let params = this.qs.stringify({
          currencyType: 1,
          uuid: this.userId,
          pn: this.page,
          pageSize: this.pageSize,
          startTime: this.dataValue[0] == '' ? '2000-12-30 00:00:00' : this.dataValue[0],
          endTime: this.dataValue[1] == '' ? '2200-12-30 00:00:00' : this.dataValue[1]
        })

        this.axios.post('statistics/quantitativeTreasure', params, {
          headers: {
            Authorization: 'bearer ' + token
          }
        }).then(res => {
          // console.log(res)
          if (res.data.code == 8008) {
            this.ethList = res.data.data.earningsList
            this.info1 = this.ethList.length
            this.$Message.info(res.data.message)
          } else if (res.data.code == 4004) {
            this.$Message.info(res.data.message)
            this.ethList = []
            this.info1 = 0
          } else if (res.data.code == 5501) {
            this.$Message.info(res.data.message)
            localStorage.removeItem('mytoken')
            this.$router.push({name: 'login'})
          }
        })
      } else if (this.bizhong == 1) {
        this.btc = true
        this.eth = false
        this.fenye = true
        this.fenye1 = false
        // console.log(111)
        if ((this.dataValue == undefined || this.dataValue[0] == '' || this.dataValue.length == 0) && this.userId == '') {
          this.pageSize = 20
          this.getBList()
          return
        }
        this.pageSize = 100
        let token = localStorage.getItem('mytoken')
        let params = this.qs.stringify({
          currencyType: 2,
          uuid: this.userId,
          pn: this.page,
          pageSize: this.pageSize,
          startTime: this.dataValue[0] == '' ? '2000-12-30 00:00:00' : this.dataValue[0],
          endTime: this.dataValue[1] == '' ? '2200-12-30 00:00:00' : this.dataValue[1]
        })
        // console.log(params)
        this.axios.post('statistics/quantitativeTreasure', params, {
          headers: {
            Authorization: 'bearer ' + token
          }
        }).then(res => {
          // console.log(res)
          if (res.data.code == 8008) {
            this.btcList = res.data.data.earningsList
            this.info = this.btcList.length
            this.$Message.info(res.data.message)
          } else if (res.data.code == 4004) {
            this.$Message.info(res.data.message)
            this.btcList = []
            this.info = 0
          } else if (res.data.code == 5501) {
            this.$Message.info(res.data.message)
            localStorage.removeItem('mytoken')
            this.$router.push({name: 'login'})
          }
        })
      }
    },
    // 分页
    changepage (page) {
      this.page = page
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        currencyType: 2,
        pn: page,
        pageSize: this.pageSize
      })
      // console.log(params)
      this.axios.post('statistics/quantitativeTreasure', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        // console.log(res)
        if (res.data.code == 8008) {
          this.btcList = res.data.data.earningsList
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({name: 'login'})
        }
      })
    },
    // 分页
    changepage1 (page) {
      this.page = page
      let token = localStorage.getItem('mytoken')
      let params = this.qs.stringify({
        currencyType: 1,
        pn: page,
        pageSize: this.pageSize
      })
      // console.log(params)
      this.axios.post('statistics/quantitativeTreasure', params, {
        headers: {
          Authorization: 'bearer ' + token
        }
      }).then(res => {
        // console.log(res)
        if (res.data.code == 8008) {
          this.ethList = res.data.data.earningsList
        } else if (res.data.code == 5501) {
          this.$Message.info(res.data.message)
          localStorage.removeItem('mytoken')
          this.$router.push({name: 'login'})
        }
      })
    }
  },
  created () {
    this.getBList()
    setTimeout(() => {
      this.getEList()
    }, 500)
  },
  mounted () {
    this.$Message.config({
      top: 250
    })
  }
}
</script>
